package com.xxyy.mylive.ui.activity;


import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.orhanobut.logger.Logger;
import com.pili.pldroid.player.PLMediaPlayer;
import com.xxyy.mylive.R;
import com.xxyy.mylive.tools.UiTool;
import com.xxyy.mylive.ui.BaseActivity;
import com.xxyy.mylive.ui.manager.VideoPlayManager;

import pl.droidsonroids.gif.GifImageView;

public class VideoActivity extends BaseActivity implements PLMediaPlayer.OnPreparedListener
        , PLMediaPlayer.OnInfoListener, PLMediaPlayer.OnCompletionListener, PLMediaPlayer.OnErrorListener {
    private ImageButton btnBack;
    private Button btnRotate;
    private GifImageView gifImageView;
    private VideoPlayManager mVideoPlayManager;

    private String mPlayRtmpURL;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_video);
    }

    @Override
    protected void initUI() {
        btnBack = (ImageButton) findViewById(R.id.btn_back);
        btnRotate = (Button) findViewById(R.id.btn_rotate);
        gifImageView = (GifImageView) findViewById(R.id.loading_gif_view);
        mVideoPlayManager = new VideoPlayManager(this, R.id.video_view);
    }

    @Override
    protected void setListener() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        btnRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mVideoPlayManager.setRotate(90);
            }
        });
    }

    @Override
    protected void initData() {
        mPlayRtmpURL = getIntent().getStringExtra("play_stream");
        Logger.i("播放rtmp地址：" + mPlayRtmpURL);
        mVideoPlayManager.setLoadingGIf(gifImageView);
        mVideoPlayManager.setPlayUrl(mPlayRtmpURL);
        mVideoPlayManager.startPlay();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoPlayManager.stopPlay();
    }

    @Override
    public void onPrepared(PLMediaPlayer plMediaPlayer) {

    }

    @Override
    public boolean onInfo(PLMediaPlayer plMediaPlayer, int i, int i1) {
        return false;
    }

    @Override
    public void onCompletion(PLMediaPlayer plMediaPlayer) {
        // TODO: 2016/10/21 这里先请求服务器主播直播是否结束。如果没有结束，开始重连。如果结束，关闭播放器。
        mVideoPlayManager.stopPlay();
    }

    @Override
    public boolean onError(PLMediaPlayer plMediaPlayer, int i) {
        boolean isHandle = false;
        switch (i) {
            case -5:
                UiTool.showToast(VideoActivity.this, "网络异常，重新连接中·········");
                break;
            case -11:
                UiTool.showToast(VideoActivity.this, "与服务器断开，重新连接中·········");
                mVideoPlayManager.setPlayUrl(mPlayRtmpURL);
                mVideoPlayManager.startPlay();
                isHandle = true;
                break;
            case -541478725:
                UiTool.showToast(VideoActivity.this, "播放列表为空");
                break;
            case -875574520:
                UiTool.showToast(VideoActivity.this, "直播不存在，断开连接");
                mVideoPlayManager.stopPlay();
                isHandle = false;
                break;
        }
        return isHandle;
    }
}
